package com.tg.msgex.helper;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import com.alibaba.fastjson.JSONObject;
import com.tg.appcommon.android.TGLog;
import com.tg.data.http.entity.MessageDataBean;
import com.tg.data.http.entity.AlertMessageFilterBean;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

@Deprecated // TODO： 使用 DeviceMessageQuery 替代此功能
public class MessageHelper {

    @Deprecated // TODO： 使用 DeviceMessageQuery 替代此功能
    public static void getMessageList(JSONObject object, @NonNull MessageListener<MessageDataBean> messageListener){
        TGHttp.getInstance().getMessageList(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<MessageDataBean>() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    protected void onSuccess(MessageDataBean content) {
                        messageListener.onSuccess(content);
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        super.onResponseError(errorCode, errorInfo);
                        messageListener.onError(errorCode, errorInfo);
                    }

                    @Override
                    protected void onOtherError(String error) {
                        super.onOtherError(error);
                        messageListener.onError(-999, error);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        messageListener.onFinish();
                    }
                });
    }

    @Deprecated // TODO： 使用 DeviceMessageQuery 替代此功能
    public static void getMessageFilter(String uuid, @NonNull MessageListener<List<AlertMessageFilterBean>> messageListener){
        HashMap<String, String> map = new HashMap<>();
        map.put("uuid", uuid);
        TGHttp.getInstance().getMessageFilter(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<List<AlertMessageFilterBean>>() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    protected void onSuccess(List<AlertMessageFilterBean> content) {
                        messageListener.onSuccess(content);
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        super.onResponseError(errorCode, errorInfo);
                        messageListener.onError(errorCode, errorInfo);
                    }

                    @Override
                    protected void onOtherError(String error) {
                        super.onOtherError(error);
                        messageListener.onError(-999, error);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        messageListener.onFinish();
                    }
                });
    }

    public abstract static class MessageListener<T> {
        protected abstract void onSuccess(T content);
        protected void onError(int errorCode, String errorInfo){
            TGLog.d("errorCode = " + errorCode + ", errorInfo = " + errorInfo);
        }
        protected void onFinish(){
            TGLog.d("onFinish");
        }
    }
}
